//
//  ToolBarView.swift
//  qfxCore
//
//  Created by QFX on 2023/3/9.
//
import UIKit

open class ToolBarView: UIView {
    typealias CustomClosures = (_ titleLabel: UILabel, _ cancleBtn: UIButton, _ doneBtn: UIButton) -> Void
    public typealias BtnAction = () -> Void
    
    open var title = "请选择" {
        didSet {
            titleLabel.text = title
        }
    }
    
    open var doneAction: BtnAction?
    open var cancelAction: BtnAction?
    // 用来产生上下分割线的效果
    fileprivate lazy var contentView: UIView = {
        let content = UIView()
        content.backgroundColor = .clear
        return content
    }()
    // 文本框
    fileprivate lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.black
        label.font = UIFont.boldSystemFont(ofSize: 15)
        label.textAlignment = .center
        return label
    }()
    
    // 文本框
//    fileprivate lazy var bottomLine: UILabel = {
//        let label = UILabel()
//        label.backgroundColor = UIColor.init(hex: "#f5f5f5")
//        return label
//    }()
    
    // 取消按钮
    open lazy var cancelBtn: UIButton = {
        let btn = UIButton()
        btn.setTitle("取消", for: UIControl.State())
        btn.titleLabel?.font = UIFont.boldSystemFont(ofSize: 16)
        btn.titleLabel?.adjustsFontSizeToFitWidth = true
        btn.setTitleColor(UIColor(hexString: "#0c0c0c"), for: UIControl.State())
        return btn
    }()
    
    // 完成按钮
    open lazy var doneBtn: UIButton = {
        let donebtn = UIButton()
        donebtn.setTitle("完成", for: UIControl.State())
        donebtn.titleLabel?.font = UIFont.boldSystemFont(ofSize: 16)
        donebtn.titleLabel?.adjustsFontSizeToFitWidth = true
        donebtn.setTitleColor(UIColor(hexString: "#0c0c0c"), for: UIControl.State())
        return donebtn
    }()
    
    override public init(frame: CGRect) {
        super.init(frame: frame)
        commonInit()
    }
    
    required public init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate func commonInit() {
        addSubview(contentView)
        contentView.addSubview(cancelBtn)
        contentView.addSubview(doneBtn)
        contentView.addSubview(titleLabel)
//        contentView.addSubview(bottomLine)
        
        doneBtn.addTarget(self, action: #selector(self.doneBtnOnClick(_:)), for: .touchUpInside)
        cancelBtn.addTarget(self, action: #selector(self.cancelBtnOnClick(_:)), for: .touchUpInside)
    }
    
    @objc func doneBtnOnClick(_ sender: UIButton) {
        doneAction?()
    }
    @objc func cancelBtnOnClick(_ sender: UIButton) {
        cancelAction?()
    }
    
    override open func layoutSubviews() {
        super.layoutSubviews()
        let margin = 15.0
        let contentHeight = Double(bounds.size.height)
        contentView.frame = CGRect(x: 0.0, y: 0, width: Double(bounds.size.width), height: contentHeight)
        let btnWidth = contentHeight + 20

        cancelBtn.frame = CGRect(x: margin, y: 0, width: btnWidth, height: btnWidth)
        doneBtn.frame = CGRect(x: Double(bounds.size.width) - btnWidth - margin, y: 0, width: btnWidth, height: btnWidth)
        let titleX = Double(cancelBtn.frame.maxX) + margin
        let titleW = Double(bounds.size.width) - titleX - btnWidth - margin
        
        titleLabel.frame = CGRect(x: titleX, y: 0, width: titleW, height: btnWidth)
//        bottomLine.frame = CGRect(x: 0.0, y: btnWidth - 1.0, width: Double(bounds.size.width), height: 1.0)
    }
}
